package com.mmd.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Splitter;
import com.mmd.bean.CODE;
import com.mmd.common.entity.MmdDict;
import com.mmd.common.entity.SuDelivery;
import com.mmd.common.entity.SuDeliveryTrack;
import com.mmd.common.entity.SuOrder;
import com.mmd.common.enums.DictTypeEnums;
import com.mmd.exception.GameException;
import com.mmd.intercepter.AdminInterceptor;
import com.mmd.manage.dao.*;
import com.mmd.manage.service.DeliveryService;
import com.mmd.manage.vo.PageData;
import com.mmd.manage.vo.QueryDeliveryListEo;
import com.mmd.manage.vo.SuDeliveryVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DeliveryServiceImpl implements DeliveryService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SuDeliveryDao suDeliveryDao;
    @Autowired
    private SuDeliveryTrackDao suDeliveryTrackDao;
    @Autowired
    private SuOrderDao suOrderDao;
    @Autowired
    private MmdDictDao mmdDictDao;
    @Autowired
    private MmdOrderDao mmdOrderDao;


    @Override
    @Transactional
    public boolean saveDeliveryNo(String batchNos, String deliveryNo) {
        long uid = AdminInterceptor.getUserLoginContext().getUid();

        if (StringUtils.isBlank(batchNos)) {
            throw new GameException(new CODE(9999, "请勾选主订单"));
        }

        SuDelivery suDelivery = suDeliveryDao.selectByPrimaryKey(deliveryNo);
        if (suDelivery != null) {
            throw new GameException(new CODE(9999, "该提单号已保存"));
        }

        List<String> batchNoList = Splitter.on(",").splitToList(batchNos);
        if (StringUtils.isBlank(deliveryNo)) {
            for (String batchNo : batchNoList) {
                suOrderDao.updateDeliveryNo2Null(batchNo);
            }
        } else {
            for (String batchNo : batchNoList) {
                SuOrder suOrder = new SuOrder();
                suOrder.setBatchNo(batchNo);
                suOrder.setDeliveryNo(deliveryNo);
                suOrderDao.updateByPrimaryKeySelective(suOrder);
            }
            SuDelivery newDelivery = new SuDelivery();
            newDelivery.setDeliveryNo(deliveryNo);
            newDelivery.setUid(uid);
            newDelivery.setCreateTime(new Date());
            suDeliveryDao.insert(newDelivery);
        }
        return true;
    }


    @Override
    public PageData<SuDeliveryVo> queryDeliveryList(QueryDeliveryListEo eo) {
        eo.setUid(AdminInterceptor.getUserLoginContext().getUid());
        eo.setStartLimit((eo.getCurPage() - 1) * eo.getPageSize());
        List<SuDeliveryVo> list = suDeliveryDao.pageList(eo);
        PageInfo<SuDeliveryVo> result = new PageInfo<>(list);
        Integer count = suDeliveryDao.countList(eo);
        result.setTotal(count);
        PageData<SuDeliveryVo> data = new PageData<>(result);
        data.setCurPage(eo.getCurPage());
        data.setPageSize(eo.getPageSize());
        return data;
    }

    @Override
    public List<SuDeliveryTrack> queryDeliveryTrackList(String deliveryNo) {
        List<SuDeliveryTrack> list = suDeliveryTrackDao.list(deliveryNo);
        return list;
    }

    @Override
    @Transactional
    public boolean saveDeliveryTrack(String deliveryNo, Integer dictKey, String content) {
        SuDelivery suDelivery = suDeliveryDao.selectByPrimaryKey(deliveryNo);
        if (suDelivery == null) {
            throw new GameException(new CODE(9999, "该提单不存在"));
        }
        if (dictKey == -1 && StringUtils.isBlank(content)) {
            throw new GameException(new CODE(9999, "请选择或者输入物流轨迹"));
        }
        SuDeliveryTrack track = new SuDeliveryTrack();
        track.setDeliveryNo(deliveryNo);
        track.setTrackTime(new Date());
        if (dictKey == -1) {
            track.setTrackDesc(content);
        } else {
            MmdDict dict = mmdDictDao.selectByKeyAndType(dictKey, DictTypeEnums.delivery_track.getType());
            if (dict == null) {
                throw new GameException(new CODE(9999, "该轨迹不存在"));
            }
            track.setTrackDesc(dict.getDictDesc());
        }
        suDeliveryTrackDao.insert(track);
        return true;
    }


    @Override
    public boolean deleteDeliveryTrack(Long trackId) {
        return suDeliveryTrackDao.deleteDeliveryTrack(trackId);
    }
}
